import { Component } from "@angular/core";
import {
  IonicPage,
  NavController,
  NavParams,
  ToastController
} from "ionic-angular";
import { TeamSeriveProvider } from "../../../../providers/team-serive/team-serive";
import { BaseUI } from "../../../../common/baseUI";
/**
 * Generated class for the TeamDetailsPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: "page-team-details",
  templateUrl: "team-details.html"
})
export class TeamDetailsPage extends BaseUI {
  //团队名称
  teamName: string;
  //团队人数
  teamNumber: string;
  //团队头像
  teamPhoto: string;
  //团队编号
  teamcode: number;
  //使用者编号
  usercode: number;
  //团队成员
  teamMemberDatas: Array<any> = [];

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public teamSeriveProvider: TeamSeriveProvider,
    public toastCtrl: ToastController
  ) {
    super();
  }

  ionViewDidLoad() {
    //初始化
    // this.init();
  }

  init() {
    //请求团队信息
    this.teamSeriveProvider.getTeamDetails(1).subscribe(
      data => {
        if (data != null) {
          if (data.status != 500) {
            this.teamName = data.team.teamname;
            this.teamNumber = data.team.teamcount + "人";
            this.teamPhoto = data.team.teamicon;
            this.teamMemberDatas = data.user;
            return;
          }
          //未登陆
          super.showToast(this.toastCtrl, "登录失效，请重新登录");
          this.navCtrl.setRoot("LoginPage");
        }
      },
      err => {
        super.showToast(this.toastCtrl, "请求数据失败，请重试");
      }
    );
  }

  join() {
    this.navCtrl.push("TeamNewPage");
  }

  /**
   * 点击用户头像
   * @param qq 头像 
   * @param nickname 称呼 
   */
  openMemberDetails(qq, nickname) {
    this.navCtrl.push("MemberDetailsPage", {
      qq,
      nickname
    });
  }

  //加入团队
  openNewApply() {
    this.teamSeriveProvider.joinTeam(1, 2).subscribe(
      data => {
        if (data != null) {
          if (data.status != 500) {
            return;
          }
          //未登陆
          super.showToast(this.toastCtrl, "登录失效，请重新登录");
          this.navCtrl.setRoot("LoginPage");
        }
      },
      err => {
        super.showToast(this.toastCtrl, "请求数据失败，请重试");
      }
    );
    this.navCtrl.push("TeamApplyPage");
  }
}
